package com.ms.p2p.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.enums.LoanStatus;
import com.ms.p2p.provider.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by chensk on 2017/7/18.
 */
@Controller
@RequestMapping({ "/member/bid" })
public class MemberBidController extends  BaseController{
    @Reference
    private LoanTenderRecordProvider loanTenderRecordProvider;
    @Reference
    private LoanProvider loanProvider;
    @Reference
    private LoanStayProvider loanStayProvider;
    @Reference
    private LoanCategoryProvider loanCategoryProvider;
    @Reference
    private LoanDueInProvider loanDueInProvider;
    /**
     * 审核中
     */
    @RequestMapping({ "/auditing" })
    public String auditing(HttpServletRequest request,
                             HttpServletResponse response, HttpSession session) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> borrows = loanProvider.findPager(null, null, user.getId(), null, LoanStatus.STATUS_0.getValue(), null, null, null, null, null, null, null, null, null, page);
        page.setTotalCount(loanProvider.findCount(null, null, user.getId(), null,  LoanStatus.STATUS_0.getValue(), null, null, null, null, null, null, null, null, null));
        request.setAttribute("borrows", borrows);
        request.setAttribute("page", page);
        return "/customer/member/loan/borrow/auditing";
    }


    /**
     * 审核中
     */
    @RequestMapping({ "/pretender" })
    public String pretender(HttpServletRequest request,
                           HttpServletResponse response, HttpSession session) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> borrows = loanProvider.findPager(null, null, user.getId(), null, LoanStatus.STATUS_2.getValue(), null, null, null, null, null, null, null, null, null, page);
        page.setTotalCount(loanProvider.findCount(null, null, user.getId(), null,  LoanStatus.STATUS_2.getValue(), null, null, null, null, null, null, null, null, null));
        request.setAttribute("borrows", borrows);
        request.setAttribute("page", page);
        return "/customer/member/loan/borrow/pretender";
    }
    /**
     * 审核中
     */
    @RequestMapping({ "/bidding" })
    public String borrowlist(HttpServletRequest request,
                             HttpServletResponse response, HttpSession session) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> borrows = loanProvider.findPager(null, null, user.getId(), null, LoanStatus.STATUS_3.getValue(), null, null, null, null, null, null, null, null, null, page);
        page.setTotalCount(loanProvider.findCount(null, null, user.getId(), null,  LoanStatus.STATUS_3.getValue(), null, null, null, null, null, null, null, null, null));
        request.setAttribute("borrows", borrows);
        request.setAttribute("page", page);
        return "/customer/member/loan/borrow/bidding";
    }
    /**
     * 审核中
     */
    @RequestMapping({ "/forlending" })
    public String forlending(HttpServletRequest request,
                             HttpServletResponse response, HttpSession session) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> borrows = loanProvider.findPager(null, null, user.getId(), null, LoanStatus.STATUS_4.getValue(), null, null, null, null, null, null, null, null, null, page);
        page.setTotalCount(loanProvider.findCount(null, null, user.getId(), null,  LoanStatus.STATUS_4.getValue(), null, null, null, null, null, null, null, null, null));
        request.setAttribute("borrows", borrows);
        request.setAttribute("page", page);
        return "/customer/member/loan/borrow/forlending";
    }
    /**
     * 审核中
     */
    @RequestMapping({ "/payment" })
    public String payment(HttpServletRequest request,
                             HttpServletResponse response, HttpSession session) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> borrows = loanProvider.findPager(null, null, user.getId(), null, LoanStatus.STATUS_5.getValue(), null, null, null, null, null, null, null, null, null, page);
        page.setTotalCount(loanProvider.findCount(null, null, user.getId(), null,  LoanStatus.STATUS_5.getValue(), null, null, null, null, null, null, null, null, null));
        request.setAttribute("borrows", borrows);
        request.setAttribute("page", page);
        return "/customer/member/loan/borrow/payment";
    }
    /**
     * 审核中
     */
    @RequestMapping({ "/completed" })
    public String completed(HttpServletRequest request,
                             HttpServletResponse response, HttpSession session) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> borrows = loanProvider.findPager(null, null, user.getId(), null, LoanStatus.STATUS_6.getValue(), null, null, null, null, null, null, null, null, null, page);
        page.setTotalCount(loanProvider.findCount(null, null, user.getId(), null,  LoanStatus.STATUS_6.getValue(), null, null, null, null, null, null, null, null, null));
        request.setAttribute("borrows", borrows);
        request.setAttribute("page", page);
        return "/customer/member/loan/borrow/completed";
    }

    @RequestMapping({ "/failed" })
    public String failed(HttpServletRequest request,
                            HttpServletResponse response, HttpSession session) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> borrows = loanProvider.findPager(null, null, user.getId(), null, LoanStatus.STATUS_M5.getValue(), null, null, null, null, null, null, null, null, null, page);
        page.setTotalCount(loanProvider.findCount(null, null, user.getId(), null,  LoanStatus.STATUS_M5.getValue(), null, null, null, null, null, null, null, null, null));
        request.setAttribute("borrows", borrows);
        request.setAttribute("page", page);
        return "/customer/member/loan/borrow/failed";
    }




    @RequestMapping({ "/stay" })
    public String repaylist(HttpServletRequest request,
                            HttpServletResponse response, HttpSession session,String billStatus) throws Exception {
        Member user = getCurrentUser();
        request.setAttribute("borrowTypes", loanCategoryProvider.findAll());
        PageBean page = new PageBean();
        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !("".equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<Map<String, Object>> repayments = loanStayProvider.findPager(user.getId()+"",null,null,billStatus,null,null,null,page);
        page.setTotalCount(loanStayProvider

                .findCount(user.getId()+"",null,null,null,null,null,null));
        request.setAttribute("repayments", repayments);
        request.setAttribute("page", page);
        request.setAttribute("billStatus",billStatus);
        return "/customer/member/loan/repay/list";
    }
}
